﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetCodeConversionRuleList", "1.0")]
    public class GetCodeConversionRuleListAction : BaseAction<GetCodeConversionRuleListParams, GetCodeConversionRuleListResult>
    {
        protected override ActionResult<GetCodeConversionRuleListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetCodeConversionRuleListParams opParams)
        {
            SER.CodeConversionRuleService service = new SER.CodeConversionRuleService();
            ListResult<VIW.VCodeConversionRule> serResult = service.GetCodeConversionRules(opParams.DataTableId, opParams.DataColumnId);
            if (serResult.IsSuccess)
            {
                GetCodeConversionRuleListResult result = new GetCodeConversionRuleListResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetCodeConversionRuleListResult.Item item = new GetCodeConversionRuleListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.DimId = serResult.Data[index].DimId;
                    item.DimCode = serResult.Data[index].DimCode;
                    item.DimName = serResult.Data[index].DimName;
                    item.DimItemId = serResult.Data[index].DimItemId;
                    item.DimItemCode = serResult.Data[index].DimItemCode;
                    item.DimItemName = serResult.Data[index].DimItemName;
                    item.RawCodes = serResult.Data[index].RawCodes;
                    result.Items.Add(item);
                }
                return new ActionResult<GetCodeConversionRuleListResult>(result);
            }
            else
            {
                return new ActionResult<GetCodeConversionRuleListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetCodeConversionRuleListParams
    {
        [Parameter]
        public Guid DataTableId { get; set; }

        [Parameter]
        public Guid DataColumnId { get; set; }
    }

    public class GetCodeConversionRuleListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            /// <summary>
            /// 对应的维度id
            /// </summary>
            public Guid DimId { get; set; }

            public string DimCode { get; set; }

            public string DimName { get; set; }

            /// <summary>
            /// 对应维度项id
            /// </summary>
            public Guid DimItemId { get; set; }

            public string DimItemCode { get; set; }

            public string DimItemName { get; set; }

            /// <summary>
            /// 原始编码
            /// </summary>
            public string RawCodes { get; set; }
        }
    }
}
